package com.bw.controller;


import com.alibaba.excel.EasyExcel;
import com.bw.Mycommon.Mycommon;
import com.bw.aop.SysRoleAop;
import com.bw.config.PageBean;
import com.bw.config.R;
import com.bw.config.UploadMinioUtils;
import com.bw.domain.SysRole;
import com.bw.domain.Zxt;
import com.bw.service.SysRoleService;
import com.bw.service.TuService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.netflix.ribbon.proxy.annotation.Http;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.*;

@RestController
@RequestMapping("SysRole")
public class SysRoleController {

    @Autowired
    SysRoleService sysRoleService;

    @Autowired
    TuService tuService;

    @RequestMapping("list")
    public R list(@RequestBody SysRole sysRole, PageBean pageBean){
        String[] createTimes = sysRole.getCreateTimes();
        if (createTimes != null && createTimes.length >0 ) {

            System.out.println("创建时间的区间查询="+sysRole.getCreateTimes());

            sysRole.setMin(createTimes[0]);
            sysRole.setMax(createTimes[1]);
        }
        PageHelper.startPage(pageBean);
        List<SysRole> list = sysRoleService.list(sysRole);
        PageInfo<SysRole> pageInfo = new PageInfo<>(list);
        return R.successed(pageInfo);
    }
    @RequestMapping("roleList")
    public R roleList(){
        List<SysRole>list=sysRoleService.RoleList();
        return R.successed(list);
    }

    @RequestMapping("update")
    public R update(@RequestBody SysRole sysRole){
        boolean update = sysRoleService.update(sysRole);
        return update?R.successed("修改成功"):R.failed("修改失败");
    }

    @RequestMapping("add")
    public R add(@RequestBody SysRole sysRole){
        System.err.println("add:"+sysRole);
        sysRole.setCreateTime(new Date());
        boolean save = sysRoleService.add(sysRole);
        return save?R.successed("添加成功"):R.failed("添加失败");
    }

    @RequestMapping("delete")
    public R delete(Integer roleId){
        boolean b = sysRoleService.delete(roleId);
        return b?R.successed("删除成功"):R.failed("删除失败");
    }

    @RequestMapping("exRole")
    public void exroleProductExcel(HttpServletResponse response) throws IOException {
        String fileName = URLEncoder.encode("产品信息", "UTF-8").replaceAll("\\+", "%20");
        response.setCharacterEncoding("UTF-8");
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        response.setHeader("Content-disposition","attachment;filename*=utf-8''"+fileName+".xlsx");
        List<SysRole> list=sysRoleService.RoleList();
        EasyExcel.write(response.getOutputStream(),SysRole.class).sheet("产品信息").doWrite(list);
    }

    @SysRoleAop
    @RequestMapping("/deletes")
    public Result deletes(String ids){
        List<String> asList = Arrays.asList(ids.split(","));
        sysRoleService.deletes(asList);
        return Result.success();
    }


    @RequestMapping("/upload")
    public String upload(MultipartFile file){
        String s = UploadMinioUtils.uploadPic(file);
        return s;
    }


    @PostMapping("selectRoleCount")
    public Result selectRoleCount(){
        List<Map<String,Object>> mapList =sysRoleService.selectRoleCount();
        return Result.success(mapList);
    }

    @RequestMapping("/import")
    public HashMap<String,Object> getImport(MultipartFile file){
        HashMap<String, Object> hashMap = new HashMap<>();
        try {
            EasyExcel.read(file.getInputStream(),SysRole.class,new CellDataDemoHeadDataListenerRole(sysRoleService)).sheet().doRead();
            hashMap.put("msg","成功");
        } catch (IOException e) {
            e.printStackTrace();
            hashMap.put("msg","失败");
        }
        return hashMap;
    }



}